package com.tensai.teach.beijing;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.tensai.study.java.utils.CompressUtil;
import com.tensai.study.java.utils.FileUtils;
import com.tensai.teach.beijing.model.now.Node;
import com.tensai.teach.beijing.model.now.V2X;
import com.tensai.teach.beijing.model.old.base.MapData;
import com.tensai.teach.beijing.model.old.base.second.Features;
import com.tensai.teach.beijing.model.old.base.second.features.geometry.JunctionGeo;
import com.tensai.teach.beijing.model.old.base.second.features.properties.JunctionProperties;
import com.tensai.teach.beijing.utils.CommonUtils;
import com.tensai.teach.beijing.utils.EntityServiceUtil;
import com.tensai.teach.beijing.utils.ReadFileUtil;
import com.tensai.teach.beijing.utils.files.model.Service;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Tensai
 */
@Slf4j
public class NewTest {

	/**
	 * 路口 -> 四条路16条道
	 */
	@SuppressWarnings("unused")
	private static final Map<Long, Map<Long, List<BmapPoint>>>  MAP = new HashMap<>();

	private static final String FILE_PATH = "J:/json/" + System.currentTimeMillis()+"/";


	@Test
	public void test() {
		clearOrCreate();
		MapData<JunctionProperties, JunctionGeo> junction = EntityServiceUtil.getJunction();

		assert junction != null;
		junction.getFeatures().stream()
				.filter(it-> it.getProperties().getJuncId() == 7 ||
						it.getProperties().getJuncId() == 11)
				.map(this::makeNode)
				.map(it -> V2X.builder()
						.timestamp(System.currentTimeMillis())
						// 自增
						.msgCnt(it.getId())
						.nodes(new ArrayList<Node>() {{
							add(it);
						}})
						.build())
				.forEach(it -> FileUtil.appendString(
						JSONUtil.formatJsonStr(JSON.toJSONString(it)),
						FILE_PATH + it.getMsgCnt() + ".json",
						Charset.defaultCharset()
				));

		zip();
	}

	@Test
	public void wuxi() {
		clearOrCreate();
		MapData<JunctionProperties, JunctionGeo> junction = EntityServiceUtil.getJunction();

		Service service = new Service();
		assert junction != null;
		junction.getFeatures().stream()
				.filter(it-> it.getProperties().getJuncId() == 7 ||
						it.getProperties().getJuncId() == 11)
				.map(service::makeNode)
				.map(it -> V2X.builder()
						.timestamp(System.currentTimeMillis())
						// 自增
						.msgCnt(it.getId())
						.nodes(new ArrayList<Node>() {{
							add(it);
						}})
						.build())
				.forEach(it -> {
					FileUtil.appendString(
						JSONUtil.formatJsonStr(JSON.toJSONString(it, SerializerFeature.DisableCircularReferenceDetect)),
						FILE_PATH + it.getMsgCnt() + ".json",
						Charset.defaultCharset());
					convertToXml(it);
				});

		zip();
	}

	private void convertToXml(V2X v2X) {
		try {
			File file = new File(FILE_PATH + v2X.getMsgCnt() + ".xml");
			JAXBContext jbt = JAXBContext.newInstance(V2X.class);
			Marshaller ms = jbt.createMarshaller();
			ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			log.info("\r\n");
			log.info("\r\n");
			Writer writer = new StringWriter();
			ms.marshal(v2X, writer);
			FileUtil.writeBytes(writer.toString().getBytes(), file);
		} catch (JAXBException e) {
			throw new RuntimeException(e);
		}
	}

	@Test
	public void xmlTest() throws JAXBException {
		String json = FileUtil.readString("J:\\json\\150224 -1.json", Charset.defaultCharset());
		V2X v2X = JSON.parseObject(json, V2X.class);
		JAXBContext jbt = JAXBContext.newInstance(V2X.class);
		Marshaller ms = jbt.createMarshaller();
		log.info("\r\n");
		log.info("\r\n");
		ms.marshal(v2X, System.out);

	}
	private Node makeNode(Features<JunctionProperties, JunctionGeo> junctionFeatures) {
		Long juncId = junctionFeatures.getProperties().getJuncId();
		return Node.builder()
				.id(juncId)
				.name("暂无")
				.inLinks(ReadFileUtil.readFeatJuncRel(juncId))
				.refPos(CommonUtils.getCenter(junctionFeatures.getGeometry()))
				.build();
	}



	private void clearOrCreate(){
		if (!FileUtil.exist(FILE_PATH)) {
			FileUtil.mkParentDirs(FILE_PATH);
			return;
		}
		if (!FileUtils.delAllFile(FILE_PATH)) {
			log.error("清理失败");
		}
		log.info("清理成功");
	}
	private void zip(){
		String format = "rar";

		try {
			CompressUtil.generateFile(FILE_PATH, format);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		log.info("打包完成");
	}


}


